package com.lc.cehui.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.widget.ImageViewPager;
import com.luck.picture.lib.photoview.OnViewTapListener;
import com.luck.picture.lib.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ImageBrowseActivity extends AppActivity {
    @BindView(R.id.viewPager)
    ImageViewPager viewPager;
    private List<String> imgs = new ArrayList<>();
    private String listjson;
    private List<String> img = new ArrayList<>();
    private String local;
    private String bannerList;
    private int num;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_browse;
    }

    @Override
    protected void initView() {
        listjson = getIntent().getStringExtra("list");
        imgs = JSON.parseArray(listjson, String.class);
        viewPager.setAdapter(new ImageBrowseAdapter(imgs, ImageBrowseActivity.this));
    }

    @Override
    protected void initData() {

    }

    private static class ImageBrowseAdapter extends PagerAdapter {

        private List<String> imageList;
        private Activity context;
        private PhotoView photoView;

        public ImageBrowseAdapter(List<String> imageList, Activity context) {
            this.imageList = imageList;
            this.context = context;
        }


        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = View.inflate(context, R.layout.imagebrowseviewpager, null);
            photoView = view.findViewById(R.id.photoView);
            TextView text = view.findViewById(R.id.tv_text);
            text.setText(position + 1 + "/" + imageList.size());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.placeholder(R.mipmap.ic_launcher);
            Glide.with(context).load(imageList.get(position)).into(photoView);
            //点击图片外
            photoView.setOnViewTapListener(new OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    ((ImageBrowseActivity) context).finish();
                    ((ImageBrowseActivity) context).overridePendingTransition(0, R.anim.browser_exit_anim);
                }
            });

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

}